//
//  HWPhoneRegisteredController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/9/12.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWPhoneRegisteredController.h"
#import  "Countdown.h"
#import "HWWebViewGameViewController.h"



@interface HWPhoneRegisteredController (){
    
     NSString *member_id; //推广id
    
}
@property(strong,nonatomic) UIView *registeredView;
@property(strong,nonatomic) UITextField *accounttextField;
@property(strong,nonatomic) UITextField *miaTexfeild;
@property(strong,nonatomic) UITextField *zaicemimaTeFeild;
@property(strong,nonatomic) UIImageView *icoImageView;
@property(strong,nonatomic) UIButton *jizhubutt;
@property(strong,nonatomic) NSString *sessionidStr;
@property(strong,nonatomic) NSString *agreeDealStr; // 0 代表不同意 1 。代表已同意了协议

@end

@implementation HWPhoneRegisteredController


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
     [self.navigationController setNavigationBarHidden:NO];
    // [self.navigationController setNavigationBarHidden:NO animated:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self navigaView];
    
}

- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *beijiimageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 249*CFLScaleValue+StatusBarHeight)];
    beijiimageView.image = [UIImage imageNamed:@"login_sign_bg"];
    beijiimageView.userInteractionEnabled = YES;
    [self.view addSubview:beijiimageView];
    
    [self addRegisteredView];
}


- (void)addRegisteredView {
    
    self.registeredView = [[UIView alloc]initWithFrame:CGRectMake(25, 68+NavBarHeight, kScreenW-50, 265)];
    self.registeredView.backgroundColor = [UIColor redColor];
    
    self.registeredView.backgroundColor = [UIColor whiteColor];
    self.registeredView.layer.shadowColor = [UIColor colorWithRed:26/255.0 green:113/255.0 blue:255/255.0 alpha:0.35].CGColor;
    self.registeredView.layer.shadowOffset = CGSizeMake(0,0);
    self.registeredView.layer.shadowOpacity = 1;
    self.registeredView.layer.shadowRadius = 10;
    self.registeredView.layer.cornerRadius = 6;
    self.registeredView.layer.masksToBounds = YES;
    self.registeredView.clipsToBounds = NO;
    
    [self.view addSubview:self.registeredView];
    
    
    UITextField *textfeild = [[UITextField alloc]initWithFrame:CGRectMake(15, 91, self.registeredView.width-30 , 35)];
    textfeild.placeholder = @"请输入手机号";
    
    textfeild.keyboardType = UIKeyboardTypePhonePad;
    textfeild.textColor = RGB(51, 51, 51);
    textfeild.font = [UIFont systemFontOfSize:13];
    textfeild.borderStyle =UITextBorderStyleNone;
    textfeild.layer.borderWidth = 1;
    textfeild.layer.borderColor = RGB(230, 239, 233).CGColor;
    textfeild.layer.cornerRadius = 6;
    textfeild.layer.masksToBounds = YES;
    UIView *youbinview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 19+16, 35)];
    textfeild.leftView = youbinview;
    UIImageView *imgeView = [[UIImageView alloc]initWithFrame:CGRectMake(11, 9, 16, 16)];
    imgeView.image = [UIImage imageNamed:@"sign_phone"];
    [youbinview addSubview:imgeView];
    self.accounttextField = textfeild;
    textfeild.leftViewMode = UITextFieldViewModeAlways;
    [self.registeredView addSubview:textfeild];
    
    
    UITextField *textfeild1 = [[UITextField alloc]initWithFrame:CGRectMake(15, 91+35+12,self.registeredView.width-30 , 35)];
    textfeild1.placeholder = @"请输入验证码";
    textfeild1.keyboardType = UIKeyboardTypePhonePad;
  //  textfeild1.secureTextEntry = YES;
    textfeild1.textColor = RGB(51, 51, 51);
    textfeild1.font = [UIFont systemFontOfSize:13];
    textfeild1.borderStyle =UITextBorderStyleNone;
    textfeild1.layer.borderWidth = 1;
    textfeild1.layer.borderColor = RGB(230, 239, 233).CGColor;
    textfeild1.layer.cornerRadius = 6;
    textfeild1.layer.masksToBounds = YES;
    UIView *youbinview1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 19+16, 35)];
    UIImageView *imgeView1 = [[UIImageView alloc]initWithFrame:CGRectMake(11, 9, 16, 16)];
    imgeView1.image = [UIImage imageNamed:@"sign_password"];
    
    textfeild1.leftView = youbinview1;
    
    [youbinview1 addSubview:imgeView1];
    textfeild1.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *righview4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 73+5, 35)];
    textfeild1.rightView = righview4;
    textfeild1.rightViewMode = UITextFieldViewModeAlways;
    
    //    UIImageView *imgeView4 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 9, 16, 16)];
    //    imgeView4.image = [UIImage imageNamed:@"sign_surep"];
    //    [righview4 addSubview:imgeView4];
    self.miaTexfeild = textfeild1;
    
    UIButton *xianshyanj = [UIButton buttonWithType:UIButtonTypeCustom];
    xianshyanj.frame = CGRectMake(3,0, 70, 35);
    
      NSString *yanzheng =  [HWjianzhiduiObject keylocalization:@"获取验证码"];
    [xianshyanj setTitle:yanzheng forState:UIControlStateNormal];
    
    [xianshyanj setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
   // xianshyanj.titleLabel.textAlignment = NSTextAlignmentRight;
  //  xianshyanj.backgroundColor = [UIColor redColor];
    xianshyanj.titleLabel.font = [UIFont systemFontOfSize:11];
//    [xianshyanj setBackgroundImage:[UIImage imageNamed:@"sign_surep"] forState:UIControlStateNormal];
//    [xianshyanj setBackgroundImage:[UIImage imageNamed:@"kaijisign_surep_b"] forState:UIControlStateSelected];
    
    
    [righview4 addSubview:xianshyanj];
    
    [xianshyanj addTarget:self action:@selector(xianshyanjTap:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.registeredView addSubview:textfeild1];
    
    
    
    self.icoImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.registeredView.width-85)/2, -85/2, 85, 85)];
    self.icoImageView.image = [UIImage imageNamed:@"acat"];
    self.icoImageView.layer.cornerRadius = 85/2;
    self.icoImageView.layer.masksToBounds = YES;
    self.icoImageView.layer.borderWidth = 5;
    self.icoImageView.layer.borderColor  =  RGB(255, 255, 255).CGColor;
    [self.registeredView addSubview: self.icoImageView];
    
    
    
    
    UITextField *textfeild2 = [[UITextField alloc]initWithFrame:CGRectMake(15, textfeild1.bottom+12,self.registeredView.width-30 , 35)];
    textfeild2.placeholder = @"请输入密码";
     textfeild2.keyboardType = UIKeyboardTypeDefault;
   // textfeild2.secureTextEntry= NO;
    textfeild2.secureTextEntry = YES;
    textfeild2.textColor = RGB(51, 51, 51);
    textfeild2.font = [UIFont systemFontOfSize:13];
    textfeild2.borderStyle =UITextBorderStyleNone;
    textfeild2.layer.borderWidth = 1;
    textfeild2.layer.borderColor = RGB(230, 239, 233).CGColor;
    textfeild2.layer.cornerRadius = 6;
    textfeild2.layer.masksToBounds = YES;
    UIView *youbinview2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 19+16, 35)];
    UIImageView *imgeView2 = [[UIImageView alloc]initWithFrame:CGRectMake(11, 9, 16, 16)];
    imgeView2.image = [UIImage imageNamed:@"sign_password"];
    
    textfeild2.leftView = youbinview2;
    
    [youbinview2 addSubview:imgeView2];
    textfeild2.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *righview5 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 35)];
    textfeild2.rightView = righview5;
    textfeild2.rightViewMode = UITextFieldViewModeAlways;
    
    //    UIImageView *imgeView4 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 9, 16, 16)];
    //    imgeView4.image = [UIImage imageNamed:@"sign_surep"];
    //    [righview4 addSubview:imgeView4];
    self.zaicemimaTeFeild = textfeild2;
    
    UIButton *xianshyanj1 = [UIButton buttonWithType:UIButtonTypeCustom];
    xianshyanj1.frame = CGRectMake(0,9, 16, 16);
    [xianshyanj1 setBackgroundImage:[UIImage imageNamed:@"sign_surep"] forState:UIControlStateNormal];
    [xianshyanj1 setBackgroundImage:[UIImage imageNamed:@"kaijisign_surep_b"] forState:UIControlStateSelected];
    [righview5 addSubview:xianshyanj1];
    
    [xianshyanj1 addTarget:self action:@selector(xianshyanjTap1:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.registeredView addSubview:textfeild2];
    
    
    UIButton *jizhubutt  = [UIButton setButtonWithFrame:CGRectMake((self.registeredView.width-200)/2, self.registeredView.height-45+6, 200, 30) Title:@"我已阅读并同意《用户注册协议》" Image:@"weixuanzhon_bynenn" Font:12 Color:RGB(153, 153, 153) Target:self Selector:@selector(xieYiButtonTap:)];
    self.jizhubutt = jizhubutt;
    [jizhubutt setImage:[UIImage imageNamed:@"sign_sure"] forState:UIControlStateSelected];
    self.agreeDealStr = @"0";
    //  jizhubutt.backgroundColor = [UIColor redColor];
    NSString * aStr = @"我已阅读并同意《用户注册协议》";
    [jizhubutt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:4];
    NSMutableAttributedString *str = [WMUtil attributedStringWithText:aStr withRangText:@"《用户注册协议》" withColor:RGB(26, 113, 255) withFont:[UIFont systemFontOfSize:12]];
    [jizhubutt setAttributedTitle:str forState:UIControlStateNormal];
    [self.registeredView addSubview:jizhubutt];
    
    
    
    
    
     UIButton *xiyibutton = [UIButton buttonWithType:UIButtonTypeCustom];
     xiyibutton.frame = CGRectMake(110, 0, 90, 30);
    // xiyibutton.backgroundColor = [UIColor redColor];
     [xiyibutton addTarget:self action:@selector(xiyibuttontap) forControlEvents:UIControlEventTouchUpInside];

     [jizhubutt addSubview:xiyibutton];
     

    UIButton *zhucebutt  = [UIButton setButtonWithFrame:CGRectMake(25,self.registeredView.bottom+21,kScreenW-50, 44) Title:@"注册" Image:nil Font:15 Color:RGB(255, 255, 255) Target:self Selector:@selector(zhuceTap:)];
    //  loginbutt.backgroundColor = RGB(68, 94, 233);
    [zhucebutt setBackgroundImage:[UIImage imageNamed:@"button_s"] forState:UIControlStateNormal];
    zhucebutt.layer.shadowColor = [UIColor colorWithRed:26/255.0 green:113/255.0 blue:255/255.0 alpha:0.35].CGColor;
    zhucebutt.layer.shadowOffset = CGSizeMake(0,0);
    zhucebutt.layer.shadowOpacity = 1;
    zhucebutt.layer.shadowRadius = 10;
    zhucebutt.layer.cornerRadius = 6;
    zhucebutt.layer.masksToBounds = YES;
    zhucebutt.clipsToBounds = NO;
    [self.view addSubview:zhucebutt];
    
}


- (void)navigaView  {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10,StatusBarHeight, 40, 44);
    // button.backgroundColor = [UIColor redColor];
    [button setImage:[UIImage imageNamed:@"details_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-70)/2, StatusBarHeight, 70, 44)];
    textlabel.text = @"注册";
    textlabel.textAlignment =  NSTextAlignmentCenter;
    textlabel.textColor = [UIColor whiteColor];
    textlabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:textlabel];
    
    
}

- (void)buttonTap {
    
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma  mark ------- 一些点击事件

// 获取验证码
- (void)xianshyanjTap:(UIButton *)sende {
    
//    if (![WMUtil validateMobile:self.accounttextField.text]) {
//
//        [MBProgressHUD showError:@"请输入正确的手机号"];
//        return ;
//
//
//    }
    
    
    
    if (!(self.accounttextField.text.length == 11)){
          
           [MBProgressHUD showError:@"请输入正确的手机号"];
            return ;
          
      }

    

    
    
    
  //  [SVProgressHUD show];
  //  [SVProgressHUD showWithStatus:@"正在发送手机号验证码"];
    [MBProgressHUD show:@"正在发送手机号验证码"];
    
    __weak typeof(self) weakSelf = self;
      
    NSDictionary *parametes = @{
                                    @"phone" : self.accounttextField.text,
                                    @"ios" : @"1",
                                    @"ct"  : @"app",
                                    @"ac" : @"mobile_code"
    
                                   };
    
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
          
        
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           if ([codeStr intValue] == 1) {
             //  [SVProgressHUD showSuccessWithStatus:@"发送成功"];
               
               [Countdown  StartTimeCodeBtn:sende];
               self.sessionidStr = response[@"data"];
    
               [MBProgressHUD showSuccess:@"发送验证码成功"];
               
               NSLog(@"%@",response);
               
               
           } else {
               
            //    [SVProgressHUD showInfoWithStatus:response[@"message"]];
               [MBProgressHUD showError:response[@"message"]];
                             
            NSLog(@"%@",response);
               
           }
           
         //  [SVProgressHUD dismiss];

         // [MBProgressHUD hideHUD];
           
           
       } failure:^(NSString *error) {
          // [SVProgressHUD dismiss];
           [MBProgressHUD hideHUD];
           
           
       }];
    
    
    
    
    
    DLog(@"获取验证码");
    

}


- (void)xianshyanjTap1:(UIButton *)sender {
    
    
    if (sender.selected == YES) {
        
        sender.selected = NO;
        // 密码
        self.zaicemimaTeFeild.secureTextEntry = YES;
    } else {
        
        sender.selected = YES;
        // 明文
        
        self.zaicemimaTeFeild.secureTextEntry = NO;
    }
    
    
}


#pragma mark ----- 点击注册按钮

- (void)zhuceTap:(UIButton *)sender {
    
   
    
    
//    if (![WMUtil validateMobile:self.accounttextField.text]) {
//
//        [MBProgressHUD showError:@"请输入正确的手机号"];
//        return ;
//
//
//    }
    
    
    
    if (![self.agreeDealStr isEqualToString:@"1"]){
        
          [MBProgressHUD showError:@"您还没有同意协议"];
        return; 
        
    }
    else if (!(self.miaTexfeild.text.length > 0)){
        
         [MBProgressHUD showError:@"请输入验证码"];
          return ;
        
    } else if (!(self.zaicemimaTeFeild.text.length > 5)) {
        
        [MBProgressHUD showError:@"请输入6位以上密码"];
        return ;
        
        
    }
    
    
    
    [MBProgressHUD show:@"正在注册"];
      
         __weak typeof(self) weakSelf = self;
    
    
          NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
                           
              NSString *path2 = [NSString stringWithFormat:@"%@/%@", bundlePath, @"coid"];

              NSFileManager *manager = [NSFileManager defaultManager];

      //        NSString *channel = @"";


             NSString *channel = [NSString string];
              if ([manager fileExistsAtPath:path2]) {
                  NSArray *allPath =[manager subpathsAtPath:path2];
                  //4.遍历所有的子路径
                  for (NSString *subPath in allPath) {
                      if ([subPath containsString:@"coid_"]) {
                          channel = [subPath substringFromIndex:@"coid_".length];
                          NSLog(@"文件夹名字%@+++",  channel);
                      }
                  }
              }
              if ([channel isEqual:@""]) {
                  member_id = [NSString stringWithFormat:@"0"];
              } else {
                  member_id = [NSString stringWithFormat:@"%@",channel];
              }
              NSLog(@"有值吗++%@+++", member_id);
    
    
      NSString *passwdStr = self.zaicemimaTeFeild.text;
      NSString *codeStr = self.miaTexfeild.text;
        
      NSDictionary *parametes = @{
                                      @"phone" : self.accounttextField.text,
                                      @"ios" : @"1",
                                      @"ct"  : @"app",
                                      @"ac" : @"mobile_reg",
                                      @"passwd" :passwdStr,
                                      @"sessionid" : self.sessionidStr,
                                      @"code":codeStr,
                                      @"member_id" : member_id
                                     };
      
         [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
             
            
          
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             if ([codeStr intValue] == 1) {
               //  [SVProgressHUD showSuccessWithStatus:@"发送成功"];
                 
                  [MBProgressHUD showSuccess:@"注册成功"];
                  [HWLocallyStoredData setUserID:response[@"data"]];
                
                  NSArray *Controller =   self.navigationController.childViewControllers;
                  [self.navigationController popToViewController:Controller[Controller.count-3] animated:YES];
                 
                 
             } else {
                 
              //    [SVProgressHUD showInfoWithStatus:response[@"message"]];
                   [MBProgressHUD showError:response[@"message"]];
                               
              NSLog(@"%@",response);
                 
             }
             
           //  [SVProgressHUD dismiss];

           // [MBProgressHUD hideHUD];
             
             
         } failure:^(NSString *error) {
            // [SVProgressHUD dismiss];
             [MBProgressHUD hideHUD];
             
             
         }];
      
      
    
    
    
    
    
     DLog(@"注册成功按钮"); 
}


// 用户协议是否同意
- (void)xieYiButtonTap:(UIButton *)sender {
    
    if (sender.selected == YES) {
        
        sender.selected = NO;
           self.agreeDealStr = @"0";
    } else {
        
        sender.selected = YES;
        self.agreeDealStr = @"1";
        
    }
    
    
    
}



- (void)xiyibuttontap {
    
    
    HWWebViewGameViewController *webview = [[HWWebViewGameViewController alloc]init];
    webview.urlString = [NSString stringWithFormat:@"%@?ct=zhanwu&ac=agreement&id=1&language=%@",[HWOCGlobalConst baseUrl],[HWjianzhiduiObject returnLanguageAcquisition]];
    webview.gname =@"注册协议";
    webview.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webview animated:YES];

}

@end
